//
//  ListKsViewController.swift
//  iKinds
//
//  Created by kinds on 2016/11/18.
//  Copyright © 2016年 Razeware LLC. All rights reserved.
//

import UIKit

class ListKsViewController: CenterViewController,UITableViewDelegate,UITableViewDataSource {

	@IBOutlet weak var tableView: UITableView!
	
	let cellId = "KsMainCell"
	
	var ksList = [KsMain]()
	//var tappedRow:Int?
	//var isAccessoyTapped = false
	
	//需要在DidDisappear中重新刷新menuButton,而不能放在WillAppear中刷新,否则会出现抖动
	override func viewDidDisappear(_ animated: Bool) {
		super.viewDidDisappear(animated)
		flashMenuButton()
		//防止在DetailVC中开启pan手势
		containerVC.panGesture.isEnabled = false
	}
	
	override func viewWillAppear(_ animated: Bool) {
		super.viewWillAppear(animated)
		//重新打开pan手势
		containerVC.panGesture.isEnabled = true
	}
	
	override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
		if segue.identifier == "toKsDetail"{
			
			let ksDetailVC = segue.destination as! KsDetailViewController
			let cell = sender as! UITableViewCell
			let selectedRow = tableView.indexPath(for: cell)!.row
			
			ksDetailVC.ksMain = ksList[selectedRow]
			ksDetailVC.containerVC = containerVC
			
			//menuButton.isHidden = true
		}
	}
	
	/*
	override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
		/*if identifier == "toKsDetail" && isAccessoyTapped{
			return true
		}else{
			return false
		}*/
		print(#function)
		return true
	}*/
	
    override func viewDidLoad() {
        super.viewDidLoad()

		tableView.delegate = self
		tableView.dataSource = self
		
        ksList = containerVC.getSavedKsMains()!
		enterBlock = reflashTableView
    }
	
	func reflashTableView(){
		ksList = containerVC.getSavedKsMains()!
		tableView.reloadData()
	}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
	
	//MARK: - Table View
	/*
	func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
		print(#function)
	}*/
	
	/*
	func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
		//tappedRow = indexPath.row
		//isAccessoyTapped = true
		//performSegue(withIdentifier: "toKsDetail", sender: tableView.cellForRow(at: indexPath)!)
		//_ = shouldPerformSegue(withIdentifier: "toKsDetail", sender: tableView.cellForRow(at: indexPath)!)
		print(#function)
	}*/
	
	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
		
		let ksMain = ksList[indexPath.row]
		cell.textLabel!.text = ksMain.title
		cell.detailTextLabel!.text = ksMain.dateBegin?.description
		return cell
	}
	
	func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return ksList.count
	}
}
